Page({

  data: {
    addressId:0,
    customItem:'全部',
    region:[],
    receiver:'张三',
    phone:'17211111111',
    area:'XXX街道',
    city:''
  },

  input(e){
    let data = {}
    data[e.currentTarget.dataset.key] = e.detail.value
    this.setData(data)
  },
  return:function(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  bindRegionChange: function(e){
    console.log('picker发送选择改变，携带值为',e.detail.value);
    this.setData({
      region:e.detail.value
    })
  },
  submit(){
    let regex = {
      receiver:/^[\u2E80-\u9FFF]+$/, //收货人
      phone:/^1\d{10}$/
    }
    let address = {
      memberId:wx.userInfo._id,
      receiver:this.data.receiver,
      phone:this.data.phone,
      area:this.data.area,
      city:this.data.region.join('')
    }
    // 验证收货人
    if(! address.receiver && ! /^[\u2E80-\u9FFF]+$/.test(address.receiver)){
      wx.showToast({
        title: '请输入正确的收货人',
        icon:'fail',
        duration:2000
      })
      return false
    }
    if(! address.phone || ! /^1\d{10}$/.test(address.phone)){
      wx.showToast({
        title: '请输入正确的电话号码',
        icon:'fail',
        duration:2000
      })
      return false
    }
    if(! address.city && ! this.data.addressId){
      wx.showToast({
        title: '请选择地址',
        icon:'fail',
        duration:2000
      })
      return false
    }

    if(this.data.addressId){//修改
      address.addressId = this.data.addressId
      wx.put('http://localhost:3000/wx/address',address).then(res => {
        if(!res.errorMsg){
          wx.showToast({
            title: '修改成功',
            icon:'success',
            duration:2000
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: -1,
            })
          },2000)
        }else{
          wx.showToast({
            title: res.errorMsg,
            icon:'fail',
            duration:2000
          })
        }
      })
    }else{//新增
      wx.post('http://localhost:3000/wx/address',address).then(res => {
        if(!res.errorMsg){
          wx.showToast({
            title: '新增成功',
            icon:'success',
            duration:2000
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: -1,
            })
          },2000)
        }else{
          wx.showToast({
            title: res.errorMsg,
            icon:'fail',
            duration:2000
          })
        }
      })
    }

    wx.post('http://localhost:3000/wx/address',address).then(res => {
      if(!res.errorMsg){
        wx.showToast({
          title: '新增成功',
          icon:'success',
          duration:2000
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: -1,
          })
        },2000)
      }else{
        wx.showToast({
          title: res.errorMsg,
          icon:'fail',
          duration:2000
        })
      }
    })
  },

  back(){
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },

  getAddressDetail(){
    wx.get('http://localhost:3000/wx/address/detail',{
      addressId:this.data.addressId
    }).then(res => {
      this.setData({
        receiver:res.receiver,
        phone:res.phone,
        city:res.city,
        area:res.area,
      })
    })
  },

  onLoad: function (options) {
    if(options.addressId){
      this.setData({
        addressId:options.addressId
      })
      // 获取地址详情
      this.getAddressDetail()
    }
  },

  onReady: function () {

  },








  onReachBottom: function () {

  },


  onShareAppMessage: function () {

  }
})